<?php

App::uses('AppModel', 'Model');

class BankCreditAuthorization extends AppModel {

    var $name = 'BankCreditAuthorization';
    
    public $belongsTo = array(
        'Bank' => array(
            'className' => 'Bank',
            'foreignKey' => 'bank_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Transaction' => array(
            'className' => 'Transaction',
            'foreignKey' => 'transaction_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'BankUser' => array(
            'className' => 'BankUser',
            'foreignKey' => 'bank_user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );



    public function save($data = null,$validate = true, $fieldList = array()) {
        $s = $this->sanitize($data);
        return parent::save($s);
    }

    public function  getBranchAndTellers($branch_id=null){
        $conditions = array();
        if($branch_id != null){
            $conditions['id']=$branch_id;
        }
        $list = $this->find('all', array(
            'conditions'=>$conditions,
            'contain'=>array(
                'BankUser'=>array('fields'=>array('BankUser.id','BankUser.username','BankUser.fname','BankUser.mname','BankUser.lname'))
            ),
            'recursive' => 1
        ));

        $branch_list = array();
        $branch_teller = array();
        foreach ($list as $value) {
            $branch_list[$value['BankBranch']['id']] = ucwords(strtolower($value['BankBranch']['branch_name']));
            foreach($value['BankUser'] as $usr){
                $branch_teller[$value['BankBranch']['id']][$usr['id']] = $usr;
            }
        }

        return array(
            'branch_list' => $branch_list,
            'branch_tellers' => $branch_teller
        );
    }
}